package com.tts.superstar;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.tts.superstar.Database.AccountDatabase;
import com.tts.superstar.View.BaseActivity;

public class RepasswordActivity extends BaseActivity {
    private ImageButton imageButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repassword);
        final EditText editOldPasswd = findViewById(R.id.oldPasswd);
        final EditText editNewPasswd = findViewById(R.id.newPasswd);
        imageButton = findViewById(R.id.aa);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldPasswd = editOldPasswd.getText().toString();
                String newPasswd = editNewPasswd.getText().toString();
                AccountDatabase database = new AccountDatabase(RepasswordActivity.this);
                SQLiteDatabase db = database.getWritableDatabase();
                SharedPreferences sharedPreferences = getSharedPreferences("account",MODE_PRIVATE);
                String user = sharedPreferences.getString("User", null);
                Cursor cursor = db.rawQuery("select mima from account where zhanghao = ?",new String[]{user});
                Boolean isTrue = false;
                while (cursor.moveToNext()){
                    if(cursor.getString(cursor.getColumnIndex("mima")).equals(oldPasswd)){
                        isTrue = true;
                    }
                }
                if(isTrue){
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("mima",newPasswd);
                    db.execSQL("UPDATE account SET mima = ? where zhanghao = ?",new String[]{newPasswd,user});
                    Toast.makeText(RepasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(RepasswordActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
